package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.components.TextFilter;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

import java.util.Arrays;

public class LoginAbilitySlice extends AbilitySlice {
    TextField name;
    TextField pwd;
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_login);
        //获取2个输入框组件对象
        name = (TextField) findComponentById(ResourceTable.Id_input_name);
        pwd = (TextField) findComponentById(ResourceTable.Id_input_pwd);
        //调用onFocusChange方法
        name.setFocusChangedListener(this::onFocusChange);
        pwd.setFocusChangedListener(this::onFocusChange);

    }
    public void onFocusChange(Component component, boolean b) {
        if (!b) {
            TextField t = (TextField) component;
            String value = t.getText();
            String msg = "ok";
            //默认颜色为正确的绿色
            Color color = Color.GREEN;
            //创建设置背景对象
            ShapeElement element = new ShapeElement();
            element.setCornerRadius(30);
            //设置边框 为绿
            element.setStroke(3,new RgbColor(0,255,0));
            if (value.trim().length()<6) {
                //字符串长度不符合 重新设置提示信息 和提示文字颜色
                msg="不能少于6位";
                color=Color.RED;
                /* 设置边框 为红 */
                element.setStroke(3,new RgbColor(255,0,0));
            }
            //判断给那个输入框组件的提示Text组件进行信息设置
            Text text;
            //是name输入框触发的
            if (component==name) {
                text = (Text) findComponentById(ResourceTable.Id_input_name_msg);
                //把背景加入到当前的输入框组件当中
                name.setBackground(element);
            } else {
                //pwd 输入框触发的
                text = (Text) findComponentById(ResourceTable.Id_input_pwd_msg);
                //把背景加入到当前的输入框组件当中
                pwd.setBackground(element);
            }
            //设置提示信息和文本颜色
            text.setText(msg);
            text.setTextColor(color);
        }

    }
}
